package com.anolesoft.PlanOrder.service.impl;

import com.anolesoft.PlanOrder.dto.MesBzzFcfhjhDto;
import com.anolesoft.PlanOrder.mapper.FengCunFangHuMapper;
import com.anolesoft.PlanOrder.service.FengCunFangHuService;
import com.anolesoft.PlanOrder.vo.MesBzzFcfhjhVo;
import com.anolesoft.Planexecution.entity.MesBzzOrderDo;
import com.anolesoft.amljmes.delivery.deliwebservice.dto.ChuRuKuDto;
import com.anolesoft.amljmes.delivery.deliwebservice.dto.MesBzzScjljhDto;
import com.anolesoft.amljmes.delivery.deliwebservice.service.DeliWebService;
import com.anolesoft.common.core.domain.LoginUser;
import com.anolesoft.common.security.util.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class FengCunFangHuServiceImpl implements FengCunFangHuService {
    @Autowired
    private FengCunFangHuMapper fengCunFangHuMapper;
    @Autowired
    private DeliWebService webService;

    @Override
    public List<MesBzzFcfhjhVo> selectListByCondition(MesBzzFcfhjhDto mesBzzFcfhjhDto) {
        if (mesBzzFcfhjhDto.getTuhao() != null && !mesBzzFcfhjhDto.getTuhao().equals("")) {
            mesBzzFcfhjhDto.setTuhao(mesBzzFcfhjhDto.getTuhao().trim().toUpperCase());
        }
        if (mesBzzFcfhjhDto.getJixing() != null && !mesBzzFcfhjhDto.getJixing().equals("")) {
            mesBzzFcfhjhDto.setJixing(mesBzzFcfhjhDto.getJixing().trim().toUpperCase());
        }
        List<MesBzzFcfhjhVo> mesBzzFcfhjhVo=fengCunFangHuMapper.selectPageList(mesBzzFcfhjhDto);
        return mesBzzFcfhjhVo;
    }

    //维护
    @Override
    public int weiHu(MesBzzFcfhjhDto mesBzzFcfhjhDto) {
        int count = fengCunFangHuMapper.weiHu(mesBzzFcfhjhDto);
        return count;
    }
    //取货
    @Override
    public int quHuo(MesBzzFcfhjhDto mesBzzFcfhjhDto) {
        int count = fengCunFangHuMapper.quHuo(mesBzzFcfhjhDto);
        return count;
    }
    //出库
    @Override
    @Transactional
    public int chuKu(MesBzzFcfhjhDto mesBzzFcfhjhDto) {
        int count = fengCunFangHuMapper.chuKu(mesBzzFcfhjhDto);
        // 计量计划出库时更改库存子表状态
        if (mesBzzFcfhjhDto.getPlanType() == 2) {
            ChuRuKuDto chuRuKuDto = new ChuRuKuDto();
            chuRuKuDto.setFlag("0");
            chuRuKuDto.setHgbq(mesBzzFcfhjhDto.getQualifiedLabelNumber());
            String c = webService.chuku(chuRuKuDto);
        }
        // 生成一条新的防护计划   到期时间为页面填写日期
        if (mesBzzFcfhjhDto.getPlanType() == 1) {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            mesBzzFcfhjhDto.setFanghuuser(loginUser.getUserUid());
            mesBzzFcfhjhDto.setFactory(loginUser.getFacCode());
            int a = fengCunFangHuMapper.insertNewFanghu(mesBzzFcfhjhDto);
            // 生成防护标签存入mes_ss_product_printfinfo表(无需制造记录)
            int b = fengCunFangHuMapper.insertprint(mesBzzFcfhjhDto);
            // 调用接口更改合格标签防护期
//            String hgbq = mesBzzFcfhjhDto.getQualifiedLabelNumber();
//            String fhq = mesBzzFcfhjhDto.getExpirationTime();
//            MesBzzScjljhDto mesBzzScjljhDto = new MesBzzScjljhDto();
//            mesBzzScjljhDto.setQualifiedLabelNo(hgbq);
//            mesBzzScjljhDto.setFhq(fhq);
//            try {
//                webService.getArrList(mesBzzScjljhDto);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
        }
        return count;
    }
    //完检
    @Override
    @Transactional
    public int wanJian(MesBzzFcfhjhDto mesBzzFcfhjhDto) {
        int count = fengCunFangHuMapper.wanJian(mesBzzFcfhjhDto);
        if (mesBzzFcfhjhDto.getPlanType() == 2) {
            // 调用接口更改合格标签计量期
            String hgbq = mesBzzFcfhjhDto.getQualifiedLabelNumber();
            String jlq = mesBzzFcfhjhDto.getExpirationTime();
            MesBzzScjljhDto mesBzzScjljhDto = new MesBzzScjljhDto();
            mesBzzScjljhDto.setQualifiedLabelNo(hgbq);
            mesBzzScjljhDto.setJlq(jlq);
            try {
                webService.getArrList(mesBzzScjljhDto);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return count;
    }
    //入库
    @Transactional
    @Override
    public int ruKu(MesBzzFcfhjhDto mesBzzFcfhjhDto) {
        int count = fengCunFangHuMapper.ruKu(mesBzzFcfhjhDto);
        ChuRuKuDto chuRuKuDto = new ChuRuKuDto();
        chuRuKuDto.setFlag("1");
        chuRuKuDto.setHgbq(mesBzzFcfhjhDto.getQualifiedLabelNumber());
        int a = fengCunFangHuMapper.fanghuruku(mesBzzFcfhjhDto);
        String c = webService.chuku(chuRuKuDto);
        return count;
    }

    @Override
    public List<MesBzzFcfhjhVo> selectEidName(MesBzzFcfhjhDto mesBzzFcfhjhDto) {
        List<MesBzzFcfhjhVo> mesBzzFcfhjhVo=fengCunFangHuMapper.selectEidName(mesBzzFcfhjhDto);
        return mesBzzFcfhjhVo;
    }
    // 派工
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int paiGong(MesBzzFcfhjhDto mesBzzFcfhjhDto) {
        // 加修改状态
        LoginUser loginUser = SecurityUtils.getLoginUser();
        mesBzzFcfhjhDto.setPaigongren(loginUser.getUserUid());// fcfh派工人
        MesBzzOrderDo mesBzzOrderDo = new MesBzzOrderDo();
        mesBzzOrderDo.setOperAtor(mesBzzFcfhjhDto.getDispatcher());// order操作工
        mesBzzOrderDo.setObjuid(mesBzzFcfhjhDto.getPlanobjuid());// 封存防护计划表主键
//        mesBzzFcfhjhDto.setPaigongren(mesBzzFcfhjhDto.getDispatcher());
        // 更改派工者和操作者
//        MesBzzFcfhjhDto m1 = new MesBzzFcfhjhDto();
//        MesBzzFcfhjhDto m2 = new MesBzzFcfhjhDto();
//        MesBzzFcfhjhDto m3 = new MesBzzFcfhjhDto();
//        MesBzzFcfhjhDto m4 = new MesBzzFcfhjhDto();
//        MesBzzFcfhjhDto m5 = new MesBzzFcfhjhDto();
//        MesBzzFcfhjhDto m6 = new MesBzzFcfhjhDto();
//        Date date = new Date();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//        String dateNow = sdf.format(date);
//        for (int i = 0; i < 6; i++) {
//            int shui = fengCunFangHuMapper.liuShuiHao();
//            String xulie = dateNow + shui + "";
//            switch (i) {
//                case 1:
//                    BeanUtils.copyProperties(mesBzzFcfhjhDto, m1);
//                    m1.setTiaoxingma(xulie);
//                    break;
//                case 2:
//                    BeanUtils.copyProperties(mesBzzFcfhjhDto, m2);
//                    m2.setTiaoxingma(xulie);
//                    break;
//                case 3:
//                    BeanUtils.copyProperties(mesBzzFcfhjhDto, m3);
//                    m3.setTiaoxingma(xulie);
//                    break;
//                case 4:
//                    BeanUtils.copyProperties(mesBzzFcfhjhDto, m4);
//                    m4.setTiaoxingma(xulie);
//                    break;
//                case 5:
//                    BeanUtils.copyProperties(mesBzzFcfhjhDto, m5);
//                    m5.setTiaoxingma(xulie);
//                    break;
//                case 6:
//                    BeanUtils.copyProperties(mesBzzFcfhjhDto, m6);
//                    m6.setTiaoxingma(xulie);
//                    break;
//            }
//        }
        int a = fengCunFangHuMapper.updatepaigong(mesBzzFcfhjhDto);
        int count = fengCunFangHuMapper.inMbo(mesBzzFcfhjhDto)+fengCunFangHuMapper.inMbp1(mesBzzFcfhjhDto)+fengCunFangHuMapper.inMbp2(mesBzzFcfhjhDto)+fengCunFangHuMapper.inMbp3(mesBzzFcfhjhDto)+fengCunFangHuMapper.inMbp4(mesBzzFcfhjhDto)+fengCunFangHuMapper.inMbp5(mesBzzFcfhjhDto)+fengCunFangHuMapper.inMbp6(mesBzzFcfhjhDto);
        fengCunFangHuMapper.updatecaozuozhe(mesBzzOrderDo);
        return count + a;
    }

    @Override
    public int barCodeGenerate(String factory) {
        int barCode = fengCunFangHuMapper.barCodeGenerate(factory);
        return barCode;
    }
}
